package ve.com.ucv.changedetection.tools;

import ve.com.ucv.changedetection.objects.Attribute;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: josmaguirre
 * Date: 1/22/13
 * Time: 10:52 AM
 * To change this template use File | Settings | File Templates.
 */
public class SetOperations {
    public static Set getWordsUnion(ArrayList<String> wordsO, ArrayList<String> wordsN){
        Set<String> wordsSet1 = new HashSet<String>(wordsO);
        Set<String> wordsSet2 = new HashSet<String>(wordsN);
        wordsSet1.addAll(wordsSet2);
        return wordsSet1;
    }

    public static Set getWordsIntersection(ArrayList<String> wordsO, ArrayList<String> wordsN){
        Set<String> wordsSet1 = new HashSet<String>(wordsO);
        Set<String> wordsSet2 = new HashSet<String>(wordsN);
        wordsSet1.retainAll(wordsSet2);
        return wordsSet1;
    }

    public static Set getAttributesUnion(ArrayList<Attribute> attributesO, ArrayList<Attribute> attributesN){
        Set<String> attributesOld = new HashSet<String>();
        Set<String> attributesNew = new HashSet<String>();

        for (Attribute attribute: attributesO){
            attributesOld.add(attribute.getAttName());
        }
        for (Attribute attribute: attributesN){
            attributesNew.add(attribute.getAttName());
        }
        attributesNew.addAll(attributesOld);
        return attributesNew;
    }

    public static Set getAttributesIntersection(ArrayList<Attribute> attributesO, ArrayList<Attribute> attributesN){
        Set<String> attributesOld = new HashSet<String>();
        Set<String> attributesNew = new HashSet<String>();

        for (Attribute attribute: attributesO){
            attributesOld.add(attribute.getAttName());
        }
        for (Attribute attribute: attributesN){
            attributesNew.add(attribute.getAttName());
        }
        attributesNew.retainAll(attributesOld);
        return attributesNew;
    }

    public static ArrayList<String> getMarkArray(String marks){
        ArrayList<String> types = new ArrayList<String>();
        StringTokenizer token = new StringTokenizer(marks, ";");
        while(token.hasMoreTokens()){
            types.add(token.nextToken());
        }
        return types;
    }

    public static int getCommonMarks(ArrayList<String> oldMarks, ArrayList<String> newMarks){
        int common = 0;
        if(oldMarks.size() < newMarks.size()){
            for (int i = 0; i < oldMarks.size(); i++){
                if (oldMarks.get(i).equalsIgnoreCase(newMarks.get(i)))
                    common++;
            }
        }else{
            for (int i = 0; i < newMarks.size(); i++){
                if (oldMarks.get(i).equalsIgnoreCase(newMarks.get(i)))
                    common++;
            }
        }
        return common;
    }

    public static int getMaximum(ArrayList<String> oldMarks, ArrayList<String> newMarks){
        if (oldMarks.size() < newMarks.size())
            return newMarks.size();
        else
            return oldMarks.size();
    }

    public static float getTypeDist(int common, int maximum){
        int numerator = 1, denominator = 1;

        for (int i = 0; i < common; i++){
            numerator *= Math.pow(2, i);
        }

        for (int i = 0; i < maximum ; i++){
            denominator *= Math.pow(2, i);
        }
        return ((float)numerator / (float)denominator);
    }

    public static int getWeightsSum(Set elements){
        int sum = 0;
        Iterator iter = elements.iterator();

        while (iter.hasNext()){
            sum += Weights.getattributeWeight(iter.next().toString());
        }
        return  sum;
    }

    /*public static void main(String argv[]){
        double a;
        int b=1,c = 2;
        //a = (float)b/(float)c;
        System.out.println((double)b/(double)c);
    } */
}
